package com.compty.paint.freemarker;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.core.page.Page;
import com.compty.paint.domain.Painter;
import com.compty.paint.domain.Painting;
import com.compty.paint.freemarker.support.NavigateBean;
import com.compty.paint.manager.PainterManager;
import com.compty.paint.manager.PaintingManager;

import freemarker.template.TemplateException;

/**
 * @Todo:名家名画频道生成
 * @author: 朱庆辉
 * @CreateTime:2012-3-26 下午01:52:01
 */
public class PainterStarGenerate   extends BaseGenerate {
	
	private static final long serialVersionUID = -8536513516008962513L;
	
	private PainterManager painterManager;
	private PaintingManager paintingManager;
	protected Date startDate;

	public PainterStarGenerate(Date stDate) {
		painterManager = SpringContextHolder.getBean("painterManager");
		paintingManager = SpringContextHolder.getBean("paintingManager");
		this.startDate = stDate;
	}
	

	public void generateHtml() throws TemplateException, IOException {
		generatePainterStarList();//生成名家列表页
		generatePaintingList();//生成作品列表页
		generatePainterStarDetail();//生成名家详情页及作品详情页
	}
	
	/**
	 * 生成名家列表页(全部列表、当代、近代、古代列表)
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	@SuppressWarnings("unchecked")
	private void generatePainterStarList() throws TemplateException, IOException{
		int pageSize = 1000;
		
		Page page = painterManager.findPainterStarByPage(curPage, pageSize, "1");
        List<Painter> list1 = (List<Painter>) page.getResult();
        page = painterManager.findPainterStarByPage(curPage, pageSize, "2");
        List<Painter> list2 = (List<Painter>) page.getResult();
        page = painterManager.findPainterStarByPage(curPage, pageSize, "3");
        List<Painter> list3 = (List<Painter>) page.getResult();
        page = painterManager.findPainterStarByPage(curPage, pageSize, "4");
        List<Painter> list4 = (List<Painter>) page.getResult();
        page = painterManager.findPainterStarByPage(curPage, pageSize, "5");
        List<Painter> list5 = (List<Painter>) page.getResult();
        root.put("list1", list1);
        root.put("list2", list2);
        root.put("list3", list3);
        root.put("list4", list4);
        root.put("list5", list5);
        output("painter_star.ftl", "/mj", "mj_index.html");
        
        List<Painter> painterList = painterManager.findPaintersByPeriod("1");//当代
        root.put("painterList", painterList);
        root.put("period", "当代");
        output("painter_star_period.ftl", "/mj", "mj_dd.html");
        
        painterList = painterManager.findPaintersByPeriod("2");//近代
        root.put("painterList", painterList);
        root.put("period", "近代");
        output("painter_star_period.ftl", "/mj", "mj_jd.html");
        
        painterList = painterManager.findPaintersByPeriod("3");//古代
        root.put("painterList", painterList);
        root.put("period", "古代");
        output("painter_star_period.ftl", "/mj", "mj_gd.html");
        
	}
	
	/**
	 * 生成名家详情页
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	private void generatePainterStarDetail() throws TemplateException, IOException{
		
		List<Painter> allList = painterManager.findAllPainerStar();
		int size = allList.size();
		
		for (int i = 0; i < size; i++) {
			
			Painter painter = allList.get(i);
			Long painterId = painter.getId();
			
			/************************* 生成作品文件到磁盘 begin *****************/
			List<Painting> paintingList = paintingManager.findPaintingsByPainter(painterId,startDate);
			for (Painting painting : paintingList) {
				generatePaintingOfPainterStar(painting);
			}
			/************************* 生成作品文件到磁盘 end *****************/
			
			root.put("painter", painter);
			root.put("paintingList", paintingList);
			
			output("detail_painterStar.ftl", "/mj", "mjxq_"+painterId+".html");
		}
	}
	
	/**
	 * 生成名家作品详情页
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	@SuppressWarnings("unchecked")
	private void generatePaintingOfPainterStar(Painting painting) throws TemplateException, IOException{
		
		if(painting != null){
			
			root.put("painting", painting);
			
			painting = paintingManager.get(painting.getId());
			
			Painter painter = painting.getPainter();
			
			List<Painter> painterList = painterManager.findPaintersByDecade(painter.getDecade());
			List<Map> painterReturnList = new ArrayList<Map>();
			for (Painter p : painterList) {
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("id", p.getId());
				map.put("imageid", p.getImageInfo().getId());
				map.put("suffix", p.getImageInfo().getSuffix());
				map.put("smallImagePath", p.getSmallImagePath());
				map.put("painter_name", p.getName());
				if( p.getNewestIndex() != null){
					map.put("newIndex", p.getNewestIndex().getTime_period()+"&nbsp;&nbsp;均价"+p.getNewestIndex().getPrice());
				}else{
					map.put("newIndex","&nbsp;&nbsp;均价&nbsp;&nbsp;");
				}
				if(p.getMaxIndex() != null){
					map.put("maxIndex", p.getMaxIndex().getTime_period()+"&nbsp;&nbsp;均价"+p.getMaxIndex().getPrice());	
				}else{
					map.put("maxIndex","&nbsp;&nbsp;均价&nbsp;&nbsp;");
				}
				if( p.getMinIndex() != null){
					map.put("minIndex", p.getMinIndex().getTime_period()+"&nbsp;&nbsp;均价"+p.getMinIndex().getPrice());
				}else{
					map.put("minIndex","&nbsp;&nbsp;均价&nbsp;&nbsp;");
				}
				
				List<Painting> paintingList  = paintingManager.findPaintingsByPainter(painter.getId(),startDate);
				if( paintingList.size() > 0 ){
					Painting firstPainting = paintingList.get(0);
					map.put("first_painting", firstPainting.getId());
				}
				
				painterReturnList.add(map);
			}
			
			String  ajaxData_painterList = JSONArray.fromObject(painterReturnList).toString();
			root.put("ajaxData_painterList", ajaxData_painterList);
			
			dealNavigateUrl(painting);
			
			output("detail_painting_mj.ftl", "/mj", "mjzp_"+painting.getId()+".html");
		}
		
	}
	
	/**
	 * 处理上下页导航
	 */
	
	private void generatePaintingList() throws TemplateException, IOException {
		int pageSize = 24;
		
		Page page = paintingManager.findPaintingsByPageOfPainterStar("all",1,pageSize);
		String prefix = "mjzp_index";
		String templateHtml = "all_paintings_painterstar.ftl";
		String type = "all";
		generateHtmlByType(type,page, prefix, templateHtml,pageSize);
		
		Page page1 = paintingManager.findPaintingsByPageOfPainterStar("1",1,pageSize);
		prefix = "mjzp_yh";
		type = "1";
		generateHtmlByType(type,page1, prefix, templateHtml,pageSize);

		Page page2 = paintingManager.findPaintingsByPageOfPainterStar("2",1,pageSize);
		prefix = "mjzp_gh";
		type = "2";
		generateHtmlByType(type,page2, prefix, templateHtml,pageSize);
		
	}
	
	/**
	 *@Todo:分类型生成名作列表页
	 *@author:朱庆辉
	 *@CreateTime:2012-4-12 下午03:18:39
	 * @param type (all:全部、1:油画、2:国画)
	 * @param page
	 * @param prefix 前缀
	 * @param templateHtml  模板页
	 * @param pageSize 每页显示个数
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	public void generateHtmlByType(String type,Page page,String prefix,String templateHtml,int pageSize) throws TemplateException, IOException{
		int pageCount = page.getPageCount();
		
		List<NavigateBean> navigateList = buildNavigateList(pageCount,prefix);
		
		for (int i = 1; i <= pageCount; i++) {
			 page = paintingManager.findPaintingsByPageOfPainterStar(type,i,pageSize);
			 root.put("page", page);
			 NavigateBean navigateBean = buildNavigateBean(prefix, i,pageCount,navigateList);
			 root.put("navigateBean", navigateBean);
			output(templateHtml, "/mj", prefix+"_"+i+".html");
		}
	}

	private NavigateBean buildNavigateBean(String prefix, int curPage,int pageCount,List<NavigateBean> navigateBeanList) {
		NavigateBean navigateBean = new NavigateBean();
		navigateBean.setTitle("第"+curPage+"页");
		navigateBean.setLinkUrl(prefix+"_"+curPage+".html");
		
		String pre_linkurl = "";
		String next_linkurl = "";
		if(curPage >1){
			pre_linkurl = prefix+"_"+(curPage-1)+".html";
		}
		if(curPage < pageCount ){
			next_linkurl = prefix+"_"+(curPage+1)+".html";
		}
		
		navigateBean.setPre_linkurl(pre_linkurl);
		navigateBean.setNext_linkurl(next_linkurl);
		
		navigateBean.setNavigateBeanList(navigateBeanList);
		
		return navigateBean;
	}

	private List<NavigateBean> buildNavigateList(int pageCount,String prefix) {
		List<NavigateBean> navigateList = new ArrayList<NavigateBean>();
		for (int i = 1; i <= pageCount; i++) {
			
			NavigateBean bean = new NavigateBean();
			bean.setTitle("第"+i+"页");
			bean.setLinkUrl(prefix+"_"+i+".html");
			
			navigateList.add(bean);
		}
		
		return navigateList;
	}

}
